package com.qqtech.qquan.pub.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleTopicDao;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.community.dao.CommunityMessageDao;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.mall.dao.MallProductDao;
import com.qqtech.qquan.mall.dao.MallShopDao;
import com.qqtech.qquan.mall.model.MallProduct;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.pub.dao.PubConcernDao;
import com.qqtech.qquan.pub.model.PubConcern;
import com.qqtech.qquan.pub.model.PubConcernQuery;
import com.qqtech.qquan.pub.model.PubConcernVo;
import com.qqtech.qquan.user.dao.UserTopicDao;
import com.qqtech.qquan.user.model.UserTopic;

@Service
public class PubConcernServiceImpl extends BaseServiceImpl<PubConcern>
		implements PubConcernService {
	public static final Logger log = LoggerFactory.getLogger(PubConcernServiceImpl.class);
	
	@Resource
	private PubConcernDao pubConcernDao;
	@Resource
	private CircleTopicDao CircleTopicDao;
	@Resource
	private CommunityMessageDao communityMessageDao;
	@Resource
	private UserTopicDao userTopicDao;
	@Resource
	private MallProductDao mallProductDao;
	@Resource
	private MallShopDao mallShopDao;

	@Override
	protected BaseDao<PubConcern> getBaseDao() {
		return pubConcernDao;
	}
	
	@Override
	public Integer getBeUserId(int bizzId, String bizzType) {
		if(BizzLevel2Enum.CIRCLE_TOPIC.equals(bizzType)){
			CircleTopic circleTopic = CircleTopicDao.getById(bizzId);
			if(circleTopic != null){
				return circleTopic.getCreatorId();
			}
		}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.equals(bizzType)){
			CommunityMessage communityMessage = communityMessageDao.getById(bizzId);
			if(communityMessage != null){
				return communityMessage.getCreatorId();
			}
		}else if(BizzLevel2Enum.USER_TOPIC.equals(bizzType)){
			UserTopic userTopic = userTopicDao.getById(bizzId);
			if(userTopic != null){
				return userTopic.getCreatorId();
			}
		}else if(BizzLevel2Enum.MALL_PRODUCT.equals(bizzType)){
			MallProduct mallProduct = mallProductDao.getById(bizzId);
			if(mallProduct != null){
				return mallProduct.getShopId();
			}
		}else if(BizzLevel2Enum.QSHOP_SHOP.equals(bizzType)){
			MallShopVo shop = mallShopDao.getById(bizzId);
			if(shop != null){
				return shop.getCreatorId();
			}
		}
		return null;
	}

	@Override
	public boolean isConcernedByUser(int bizzId, String bizzType, Integer userId) {
		PubConcernQuery concern = new PubConcernQuery();
		concern.setBizzId(bizzId);
		concern.setBizzType(bizzType);
		concern.setCreatorId(userId);
		return pubConcernDao.queryCount(concern) > 0;
	}
	
	@Override
	public List<PubConcernVo> myLastSomeConcerns(Integer userId, Integer some, BizzLevel2Enum bizzType) {
		PubConcernQuery query = new PubConcernQuery();
		query.setCreatorId(userId);
		if(bizzType!=null){
			query.setBizzType(bizzType.getCode());
		}
		Pageable pageKit = new PageKit(1,some, new PageOrder(Direction.DESC, "newAt"));
		return pubConcernDao.queryList(query, pageKit);
	}
}